<template>
  <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      v-loading="formLoading"
      label-width="0px"
      :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="50"/>
<!--      <el-table-column label="字段名" min-width="150">-->
<!--        <template #default="{ row, $index }">-->
<!--          <el-form-item :prop="`${$index}.field`" :rules="formRules.field" class="mb-0px!">-->
<!--            <el-input v-model="row.field" placeholder="请输入字段名"/>-->
<!--          </el-form-item>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="字段列名" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.name`" :rules="formRules.name" class="mb-0px!">
            <el-input v-model="row.name" placeholder="请输入字段列名"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="字段描述" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.columnDesc`" :rules="formRules.columnDesc" class="mb-0px!">
            <el-input v-model="row.columnDesc" placeholder="请输入字段描述"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="是否必填" width="60">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.isRequired`" :rules="formRules.isRequired" class="mb-0px!">
            <el-checkbox style="  justify-content: center; " true-label="1" false-label="0"
                         v-model="row.isRequired"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="显示类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.htmlType`" :rules="formRules.htmlType" class="mb-0px!">
            <el-select v-model="row.htmlType" placeholder="请选择显示类型">
              <el-option label="文本框" value="input"/>
              <el-option label="文本域" value="textarea"/>
              <el-option label="数字框" value="number"/>
              <el-option label="下拉框" value="select"/>
              <el-option label="单选框" value="radio"/>
              <el-option label="复选框" value="checkbox"/>
              <el-option label="日期控件" value="date"/>
              <el-option label="日期时间控件" value="dateTime"/>
              <el-option label="图片上传" value="imageUpload"/>
              <el-option label="文件上传" value="fileUpload"/>
              <el-option label="富文本控件" value="editor"/>
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="字典类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.dictType`" :rules="formRules.dictType" class="mb-0px!">
            <el-select v-model="row.dictType" placeholder="请选择字典类型">
              <el-option label="请选择字典生成" value=""/>
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="分值" min-width="100">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.score`" :rules="formRules.score" class="mb-0px!">
            <el-input v-model="row.score" placeholder="请输入分值"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="排序" min-width="100">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.sort`" :rules="formRules.sort" class="mb-0px!">
            <el-input v-model="row.sort" placeholder="请输入排序"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="数据类型" width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.dataType`" :rules="formRules.dataType" class="mb-0px!">
            <el-select v-model="row.dataType" placeholder="请选择数据类型">
              <el-option label="文本" value="string"/>
              <el-option label="整数" value="number"/>
              <el-option label="双精度浮点" value="double"/>
              <el-option label="浮点" value="float"/>
              <el-option label="日期" value="date"/>
              <el-option label="日期时间" value="datatime"/>
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加通用表单-详情</el-button>
  </el-row>
</template>
<script setup lang="ts">
import {FormInfoApi} from '@/api/business/general/form'


const props = defineProps<{
  type: String // 类型（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref<any[]>([])
const formRules = reactive({
  type: [{required: true, message: '类型不能为空', trigger: 'change'}],
  field: [{required: true, message: '字段名不能为空', trigger: 'blur'}],
  name: [{required: true, message: '字段列名不能为空', trigger: 'blur'}],
  htmlType: [{required: true, message: '显示类型不能为空', trigger: 'change'}],
  dataType: [{required: true, message: '数据类型不能为空', trigger: 'change'}],
  sort: [{required: true, message: '排序不能为空', trigger: 'blur'}]
})
const formRef = ref() // 表单 Ref


/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
    () => props.type,
    async (val) => {
      // 1. 重置表单
      formData.value = []
      // 2. val 非空，则加载数据
      if (!val) {
        return;
      }
      try {
        formLoading.value = true
        formData.value = await FormInfoApi.getFormDetailsListByType(val)
      } finally {
        formLoading.value = false
      }
    },
    {immediate: true}
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    type: undefined,
    field: props.type as any + '_' + get5DigitNumber(),
    name: undefined,
    columnDesc: undefined,
    isRequired: '0',
    htmlType: undefined,
    dictType: undefined,
    score: undefined,
    sort: undefined,
    dataType: 'string',
    remark: undefined

  }
  row.type = props.type as any
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

const get5DigitNumber = () => {
  // 生成10000到99999之间的随机整数（包含边界）
  return Math.floor(Math.random() * 90000) + 10000;
}

defineExpose({validate, getData})
</script>
